package com.luckyframe.project.jmeter.jmeterAlertConfig.domain;

import java.util.ArrayList;
import java.util.List;

import static com.luckyframe.project.jmeter.util.DoubleUtil.round;

public class AlertChecker {

    private static String message="当前%s %s  %s 预警值 %s";

    public static List<CheckResult> checkAll(JmeterAlertConfig alertConfig, AlertCheckEntity alertCheckEntity){


        List<CheckResult> results=new ArrayList<>();


        //响应时间
        if(alertCheckEntity.getAvgRespTime()> alertConfig.getAvgRespTime()){
            results.add(new CheckResult(String.format(message,new Object[]{"平均响应时间",round(alertCheckEntity.getAvgRespTime()),"超出",round(alertConfig.getAvgRespTime())}),
                    AlertType.AVG_RESPONSE_TIME,alertCheckEntity.getSampleName()));
        }

        //tps
        if(alertCheckEntity.getTps()< alertConfig.getTps()){
            results.add(new CheckResult(String.format(message,new Object[]{"tps",round(alertCheckEntity.getTps()),"小于",round(alertConfig.getTps())}),
                    AlertType.TPS,alertCheckEntity.getSampleName()));
        }

        if(alertCheckEntity.getSuccessRatio()< alertConfig.getSuccessRatio()){
            results.add(new CheckResult(String.format(message,new Object[]{"事务成功率",round(alertCheckEntity.getSuccessRatio()),"小于",round(alertConfig.getSuccessRatio())}),
                    AlertType.SUCCESS_RATIO ,alertCheckEntity.getSampleName()));
        }


        if(alertCheckEntity.getLine95()> alertConfig.getLine95()){
            results.add(new CheckResult(String.format(message,new Object[]{"用户满意度",round(alertCheckEntity.getLine95()),"超出",round(alertConfig.getLine95())}),
                    AlertType.LINE95_TIME ,alertCheckEntity.getSampleName()));
        }


        return results;
    }

    public static void main(String[] args) {

        String format = String.format(message, new Object[]{"平均响应时间", 1.01111d, "超出", 1});

        System.out.println("format = " + format);

    }

}
